package com.bootcamp.mall.service.user;

import com.bootcamp.mall.dao.OrderDao;
import com.bootcamp.mall.dao.UserDao;
import com.bootcamp.mall.model.User;
import com.bootcamp.mall.model.bo.order.ChangeOrderBO;
import com.bootcamp.mall.model.bo.user.UpdateUserDataBO;
import com.bootcamp.mall.model.vo.user.AllUserVO;
import com.bootcamp.mall.model.vo.user.UserDataVO;
import com.bootcamp.mall.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-09 20:07
 **/

public class UserServiceImpl implements UserService {
    @Override
    public List<AllUserVO> allUser() {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        UserDao mapper = sqlsession.getMapper(UserDao.class);
        //从数据库获取pojo
        List<User> users = mapper.selectAllUsers();
        // close
        sqlsession.close();
        // 如果服务器一个都没有
        if (users == null) {
            return null;
        }
        //pojo转换为vo
        List<AllUserVO> userVOs = new ArrayList<>();
        for (User user : users) {
            AllUserVO userVO = new AllUserVO();
            userVO.setId(user.getId());
            userVO.setEmail(user.getUsername());
            userVO.setNickname(user.getNickname());
            userVO.setPwd(user.getPassword());
            userVO.setRecipient(user.getRecipient());
            userVO.setAddress(user.getAddress());
            userVO.setPhone(user.getPhone());
            userVOs.add(userVO);
        }
        return userVOs;
    }

    @Override
    public Integer deleteUser(Integer id) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        UserDao mapper = sqlsession.getMapper(UserDao.class);
        // 判断是否是 admin
        String username = mapper.selectUsernameById(id);
        if ("admin".equals(username)) {
            return -1;
        }
        // 删除id
        Integer affectRows = mapper.deleteUserById(id);
        // commit and close
        sqlsession.commit();
        sqlsession.close();
        return affectRows;
    }

    @Override
    public List<AllUserVO> searchUser(String nickname) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        UserDao mapper = sqlsession.getMapper(UserDao.class);
        //从数据库获取pojo
        nickname = "%" + nickname + "%";
        List<User> users = mapper.selectUserByNickname(nickname);
        // close
        sqlsession.close();
        //pojo转换为vo
        List<AllUserVO> userVOs = new ArrayList<>();
        for (User user : users) {
            AllUserVO userVO = new AllUserVO();
            userVO.setId(user.getId());
            userVO.setEmail(user.getUsername());
            userVO.setNickname(user.getNickname());
            userVO.setPwd(user.getPassword());
            userVO.setRecipient(user.getRecipient());
            userVO.setAddress(user.getAddress());
            userVO.setPhone(user.getPhone());
            userVOs.add(userVO);
        }
        return userVOs;
    }

    @Override
    public UserDataVO showUserData(String token) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        UserDao mapper = sqlsession.getMapper(UserDao.class);
        User user = mapper.selectUserByPreciseNickname(token);
        // pojo对象映射为 vo
        UserDataVO userVO = new UserDataVO();
        userVO.setId(user.getId());
        userVO.setEmail(user.getUsername());
        userVO.setNickname(user.getNickname());
        userVO.setRecipient(user.getRecipient());
        userVO.setAddress(user.getAddress());
        userVO.setPhone(user.getPhone());
        // close
        sqlsession.close();
        return userVO;
    }

    @Override
    public Integer updateUserData(UpdateUserDataBO userBO) {
        // 判断当前用户是否为 admin
        if (userBO.getId() == 1) {
            return 1;
        }else if ("admin".equals(userBO.getNickname())){
            return 2;
        }
        // sql更新七连
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        UserDao mapper = sqlsession.getMapper(UserDao.class);
        try {
            // 设置state
            Integer affectRows = mapper.updateUser(userBO);
            System.out.println("更新用户数为：" + affectRows);
            sqlsession.commit();
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            sqlsession.rollback();
            return -1;
        } finally {
            sqlsession.close();
        }
    }
}
